package com.qianfeng.wuyang.himalaya.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.qianfeng.wuyang.himalaya.R;
import com.qianfeng.wuyang.himalaya.activity.MyApplication;
import com.qianfeng.wuyang.himalaya.adapter.monthlyadapter.AnchorMonthlyAdapter;
import com.qianfeng.wuyang.himalaya.adapter.monthlyadapter.MonthlyAdapter;
import com.qianfeng.wuyang.himalaya.bean.monthlybean.MonthlyEntity;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * A simple {@link Fragment} subclass.
 */
public class MonthlyFragment extends Fragment implements Callback<MonthlyEntity>,MonthlyAdapter.JMOnChildClickListener, View.OnClickListener {
   private ImageView fragment_monthly_im;
    private MonthlyAdapter adapter;
    private AnchorMonthlyAdapter anchoradapter;


    public MonthlyFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_monthly, container, false);

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        MyApplication application = (MyApplication) getActivity().getApplication();
        application.getMonthlyHttp().getList().enqueue(this);

        fragment_monthly_im = (ImageView) view.findViewById(R.id.fragment_monthly_im);

        fragment_monthly_im.setOnClickListener(this);
        //节目榜单
        RecyclerView recyclerView= (RecyclerView) view.findViewById(R.id.fragment_monthly_recycler_jiemu);
        adapter = new MonthlyAdapter(getContext());
        recyclerView.setAdapter(adapter);
        adapter.setListener(this);
        //解决在scrollview中滑动冲突
        recyclerView.setNestedScrollingEnabled(false);
        //解决ScrollView下嵌套RecyclerView进页面不在顶部的问题
        recyclerView.setFocusable(false);

        //主播榜单
        RecyclerView anchorrecyclerView = (RecyclerView) view.findViewById(R.id.fragment_monthly_recycler_zhubo);
        anchoradapter =new AnchorMonthlyAdapter(getContext());
        anchorrecyclerView.setAdapter(anchoradapter);
        anchorrecyclerView.setNestedScrollingEnabled(false);




    }

    @Override
    public void onResponse(Call<MonthlyEntity> call, Response<MonthlyEntity> response) {
        Picasso.with(getContext()).load(response.body().getFocusImages().getList().get(0).getPic()).into(fragment_monthly_im);
        adapter.addAll(response.body().getDatas().get(0).getList());
        anchoradapter.addAll(response.body().getDatas().get(1).getList());
    }

    @Override
    public void onFailure(Call<MonthlyEntity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(getContext(),t.getMessage(),Toast.LENGTH_SHORT).show();
    }


    @Override
    public void OnItemClick(View view) {
        Toast.makeText(getContext(),"没有二级接口",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View view) {
        Toast.makeText(getContext(),".........",Toast.LENGTH_SHORT).show();
    }
}
